package cn.itcast.listener;


import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

/**
 * @param
 * @return
 */
@WebListener
public class ServletContexAttributeListenerDemo implements ServletContextAttributeListener {//属性监听器

    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
     //监听到属性的添加就会触发此方法
        //在此获取域对象
        ServletContext servletContext = scae.getServletContext();
        //域对象获取添加的属性
        Object username = servletContext.getAttribute("username");
        System.out.println("usernameAdd = " + username);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        //监听到属性的替换（重新赋值）就会触发此方法
        //在此获取域对象
        ServletContext servletContext = scae.getServletContext();
        //域对象获取替换的的属性
        Object username = servletContext.getAttribute("username");
        System.out.println("usernameReplaced = " + username);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        //监听到属性的删除就会触发此方法
        //在此获取域对象
        ServletContext servletContext = scae.getServletContext();
        //域对象获取属性
        Object username = servletContext.getAttribute("username");
        System.out.println("usernameRemoved = " + username);
    }
}
